package tmproject.hlhj.fhp.cartrans.fragments

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import kotlinx.android.synthetic.main.fgm_notice.view.*
import tmproject.hlhj.fhp.cartrans.BaseFgm
import tmproject.hlhj.fhp.cartrans.IView.NoticeView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.activitys.NoticeDetailAty
import tmproject.hlhj.fhp.cartrans.adapter.NoticeListAdp
import tmproject.hlhj.fhp.cartrans.javaBean.NoticeListBean
import tmproject.hlhj.fhp.cartrans.presenter.NoticePresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper

/**
 * Created by Never Fear   on 2018\8\28 0028.
Never More....
 */
class NoticeFgm :BaseFgm<NoticeView,NoticePresenter>(), NoticeView, SpringView.OnFreshListener {
    private val datas=ArrayList<NoticeListBean.DataBean.ListBean>()
    private var page=1
    private var hasMore=true
    private lateinit var noticeListAdp:NoticeListAdp
    override fun bindView(): NoticeView {
        return this
    }

    override fun createPresenter(): NoticePresenter {
        return  NoticePresenter()
    }

    override fun getContentId(): Int {
        return  R.layout.fgm_notice
    }

    override fun initView() {
//        rootView.spView.footer=DefaultFooter(context)
        rootView.spView.header=DefaultHeader(context)

        noticeListAdp= NoticeListAdp(datas)

        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.recyclerview.adapter=noticeListAdp

        noticeListAdp.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                if (hasMore){
                    page++
                    presenter?.getNotices(context!!,SpHelper.getToken(context!!),page)
                }else{

                }

            }
        })
    }
    override fun logicStart() {
    }

    override fun setNoticeList(noticeListBean: NoticeListBean) {
        rootView.spView.onFinishFreshAndLoad()
        datas.addAll(noticeListBean.data.list)
        noticeListAdp.notifyDataSetChanged()
    }

    override fun setListener() {
        noticeListAdp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context, NoticeDetailAty::class.java)
            intent.putExtra("id",datas[position].id)
            startActivityForResult(intent,-1)
        }
        rootView.spView.setListener(this)
    }

    override fun onResume() {

        super.onResume()
        page=1
        datas.clear()
        presenter?.getNotices(context!!,SpHelper.getToken(context!!),page)

    }
    override fun onLoadmore() {
        page++
        presenter?.getNotices(context!!,SpHelper.getToken(context!!),page)

    }

    override fun noMore() {
        rootView.spView.onFinishFreshAndLoad()
        hasMore=false
        noticeListAdp.loadMoreEnd()
    }

    override fun onRefresh() {
        page=1
        hasMore=true
        noticeListAdp.setEnableLoadMore(true)
        datas.clear()
        noticeListAdp.setEnableLoadMore(true)
        presenter?.getNotices(context!!,SpHelper.getToken(context!!),page)

    }
}